package io.renren.modules.work.Utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.basic.entity.MtCcTaskRunInfoDetEntity;
import io.renren.modules.basic.entity.MtCcTaskRunInfoEntity;
import io.renren.modules.basic.entity.MtCcWarehousPositionRelatEntity;
import io.renren.modules.basic.entity.MtCcWarehousRankEntity;
import io.renren.modules.basic.service.MtCcTaskRunInfoDetService;
import io.renren.modules.basic.service.MtCcTaskRunInfoService;
import io.renren.modules.basic.service.MtCcWarehousPositionRelatService;
import io.renren.modules.basic.service.MtCcWarehousRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @Author: liguanghao
 * @Date: 2020/4/23 21:49
 * @Version 1.0
 * 同一库位存放多种货物专用回库方法
 */
@Component
public class BackWarehous {
    @Autowired
    private MtCcTaskRunInfoService mtCcTaskRunInfoService;
    @Autowired
    private MtCcTaskRunInfoDetService mtCcTaskRunInfoDetService;
    @Autowired
    private MtCcWarehousPositionRelatService mtCcWarehousPositionRelatService;
    @Autowired
    private MtCcWarehousRankService mtCcWarehousRankService;
    @Autowired
    private static MtCcTaskRunInfoService staticMtCcTaskRunInfoService;
    @Autowired
    private static MtCcTaskRunInfoDetService staticMtCcTaskRunInfoDetService;
    @Autowired
    private static MtCcWarehousPositionRelatService staticMtCcWarehousPositionRelatService;
    @Autowired
    private static MtCcWarehousRankService staticMtCcWarehousRankService;
    @PostConstruct
    public void init(){
        staticMtCcTaskRunInfoService=mtCcTaskRunInfoService;
        staticMtCcTaskRunInfoDetService=mtCcTaskRunInfoDetService;
        staticMtCcWarehousPositionRelatService=mtCcWarehousPositionRelatService;
        staticMtCcWarehousRankService=mtCcWarehousRankService;
    }
    public static void backWarehous(List<MtCcWarehousPositionRelatEntity> list, MtCcTaskRunInfoEntity mtCcTaskRunInfo){
        String productModel="";
        String productNum="";
        String productRemarks="";
        String productDateNum="";
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list
             ) {
            mtCcWarehousPositionRelatEntity.setStatus(3);
            staticMtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelatEntity);
            productModel=productModel.concat(mtCcWarehousPositionRelatEntity.getProductCode()).concat("+");
            productNum=productNum.concat(String.valueOf(mtCcWarehousPositionRelatEntity.getQuantity())).concat("+");
            productRemarks=productRemarks.concat(mtCcWarehousPositionRelatEntity.getRemark()).concat("+");
            productDateNum=productDateNum.concat(mtCcWarehousPositionRelatEntity.getDateNum()).concat("+");

        }
        MtCcWarehousRankEntity byId = staticMtCcWarehousRankService.getById(list.get(0).getWarehousPositionId());
        byId.setIsOccupy(1);
        staticMtCcWarehousRankService.updateById(byId);
        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity = new MtCcTaskRunInfoEntity();
        mtCcTaskRunInfoEntity.setCreateTime(new Date());
        mtCcTaskRunInfoEntity.setUserId(mtCcTaskRunInfo.getUserId());
        mtCcTaskRunInfoEntity.setUserName(mtCcTaskRunInfo.getUserName());
        mtCcTaskRunInfoEntity.setSource(mtCcTaskRunInfo.getSource());
        mtCcTaskRunInfoEntity.setTaskType(1);
        mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod
                .creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE, mtCcTaskRunInfo.getTaskCode().substring(1, 3)));
        mtCcTaskRunInfoEntity.setStatus(0);
        mtCcTaskRunInfoEntity.setProductId(list.get(0).getProductId());
        mtCcTaskRunInfoEntity.setModel(list.get(0).getProductCode());
        mtCcTaskRunInfoEntity.setNum(String.valueOf(list.get(0).getQuantity()));
        mtCcTaskRunInfoEntity.setUnit(mtCcTaskRunInfo.getUnit());
        mtCcTaskRunInfoEntity.setRemarks("回库任务,具体内容以子任务为准");
        staticMtCcTaskRunInfoService.save(mtCcTaskRunInfoEntity);//新建一条剩余货物数量的主任务;
        MtCcTaskRunInfoEntity taskInfo = staticMtCcTaskRunInfoService
                .getOne(new QueryWrapper<MtCcTaskRunInfoEntity>()
                        .eq("task_code", mtCcTaskRunInfoEntity.getTaskCode()));
        MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity1 = new MtCcTaskRunInfoDetEntity();
        mtCcTaskRunInfoDetEntity1.setCreateTime(new Date());
        mtCcTaskRunInfoDetEntity1.setWarehousRankId(list.get(0).getWarehousPositionId());
        mtCcTaskRunInfoDetEntity1.setTaskType(taskInfo.getTaskType());
        mtCcTaskRunInfoDetEntity1.setTaskDetCode(mtCcTaskRunInfoEntity.getTaskCode());
        mtCcTaskRunInfoDetEntity1.setTaskRunId(taskInfo.getId());
        mtCcTaskRunInfoDetEntity1.setStatus(0);
        mtCcTaskRunInfoDetEntity1.setModel(productModel);
        mtCcTaskRunInfoDetEntity1.setNum(productNum);
        mtCcTaskRunInfoDetEntity1.setDateNum(productDateNum);
        mtCcTaskRunInfoDetEntity1.setRemarks(productRemarks);
        mtCcTaskRunInfoDetEntity1.setUnit(mtCcTaskRunInfoEntity.getUnit());
        mtCcTaskRunInfoDetEntity1.setWarehousRankName(list.get(0).getWarehousPositionName());
        staticMtCcTaskRunInfoDetService.save(mtCcTaskRunInfoDetEntity1);//新建子任务
    }
}
